<template>
    <div>
        <div @click="popup.openPopup" class="vue-flow-model">
            <n-icon size="30" color="#18A058">
                <data-blob/>
            </n-icon>
            <Handle id="fillMissingData-inlet" type="target" :position="Position.Left"
                    :is-valid-connection="props.isValidSourcePos"/>
            <Handle id="fillMissingData-export" type="source" :position="Position.Right"
                    :is-valid-connection="props.isValidTargetPos"/>
        </div>
        <span>填补缺失值</span>
        <flow-popup-window
            :show="popup.show"
            :on-click-outside="popup.clickPopupOutside"
            :x="popup.x"
            :y="popup.y"
        >
            <n-card title="填补缺失值">
                <p>选择所需填补的字段列: </p>
                <n-select
                    v-model:value="field"
                    :options="props.data.data.title"
                    @update:value="popup.reopen"
                    multiple
                    placeholder="请选择所要清洗的字段列"
                    label-field="title"
                    value-field="key"
                />
                <p>填补方式: </p>
                <n-radio-group v-model:value="fillMethod" name="radioGroup">
                    <n-radio :value="'directly'">
                        直接填补缺失值
                    </n-radio>
                    <n-select
                        v-model:value="directlyValue"
                        :options="directly"
                        @update:value="popup.reopen"
                    />
                    <n-radio :value="'downward'">
                        向下填补缺失值(将单元格中的值向下填充至当前所选列中的相邻空单元格内)
                    </n-radio>
                </n-radio-group>
                <template #footer>
                    <n-space>
                        <n-button @click="getSuperiorData">获取上级节点数据</n-button>
                        <n-button @click="fill">填充</n-button>
                    </n-space>
                </template>
            </n-card>
        </flow-popup-window>
    </div>
</template>

<script setup>

import {Handle, Position} from "@vue-flow/core";
import {DataBlob} from "@vicons/carbon";
import {useElementsStore} from "@/stores/elements";
import workApi from "@/http/workApi";
import FlowPopupWindow from "@/components/VueFlowModel/vueFlowPopupWindow/FlowPopupWindow.vue";

const props = defineProps(['isValidTargetPos', 'isValidSourcePos', 'id', 'data'])
const elements = useElementsStore()

// 点击弹窗
const popup = reactive({
    show: false,
    x: null,
    y: null,
    wait: false,
    clickPopupOutside: () => {
        popup.closedPopup()
    },
    openPopup: (e) => {
        popup.x = e.x
        popup.y = e.y
        popup.show = true
    },
    closedPopup: () => {
        if (!popup.wait) {
            console.log(22)
            popup.show = false;
        }
    },
    reopen: () => {
        popup.show = true
    }
})
// 选中字段
const field = ref([])
// 填补方式
const fillMethod = ref("directly")
/**
 * 填充
 * */
const fill = ()=>{
    // 判断填充方式
    if (fillMethod.value === 'directly') {
        directlyFill()
    }else if (fillMethod.value === 'downward') {
        downwardFill()
    }
}
// 直接填补的选项
const directly = [
    {label: "0", value: "0"},
    {label: "平均数", value: "mean"},
    {label: "众数", value: "mode"},
    {label: "中位数", value: "median"},
]
const directlyValue = ref("mean")
/**
 * 直接填充
 * */
const directlyFill = ()=>{
    workApi.post("perform_tasks/fill_missing",{
        data:props.data.data,
        operation: {
            row: {serial_number: []},
            column: {
                serial_number: field.value,
                value:directlyValue.value
            }
        }
    }).then(r=>{
        props.data.data = r.data
    })
}
/**
 * 向下填充
 * */
const downwardFill = ()=>{
    workApi.post("perform_tasks/fill_missing_down",{
        data:props.data.data,
        operation: {
            row: {serial_number: []},
            column: {
                serial_number: field.value,
            }
        }
    }).then(r=>{
        props.data.data =r.data
    })
}
// 抽屉
const active = ref(false)

const getSuperiorData = () => {
    // 获取上级节点数据
    for (let i of elements.elements) {
        if (i['target'] === props.id) {
            for (let j of elements.elements) {
                if (j.id === i['source']) {
                    props.data.data = {...j.data.data}
                    props.data.data.title.map(s => {
                        // 移除可修改
                        delete s.render
                        return s
                    })
                    return
                }
            }
        }
    }
}

</script>

<style scoped lang="less">
.n-select {
    width: 300px;
}
</style>